package com.hf.easypoi;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * @program: testjar
 * @description: 学习EasyPoi，并写出测试代码
 * @author: hfen
 * @create: 2020-03-16 10:19
 **/
public class testEasyPoi {

    public static void main(String args[]) {
//        myExportExcel();
        myExportExcel2();

//
//        try {
//            // 测试单个sheet
//            testExportExcel();
//            // 测试多个sheet
//            testExportExcels();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    /**
    * @Description: 我学习了 掘金上的例子，对官方文档的一个方法
    * @Param: []
    * @return: void
    * @Author: hfen
    * @Date: 2020/3/16 14:21
    */
    public static void myExportExcel() {
        List<UserEntity> list = new ArrayList<>();
        int i =0;
        while (i<10) {
            UserEntity userEntity = new UserEntity();
            userEntity.setId(i+1);
            userEntity.setName("小寒" + i);
            userEntity.setAge(i);
            userEntity.setSex(1);
            userEntity.setEmail("asdfasdf");
            userEntity.setTime(new Date());
            list.add(userEntity);
            i++;
        }
        ExportParams exportParams = new ExportParams("计算机一班学生", "学生");
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, UserEntity.class,list);

        String path = "src/resources/excel/";
        String filename = "myStudent.xls";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
        }
        try (FileOutputStream fos = new FileOutputStream(path + filename)) {
            workbook.write(fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
    * @Description: 2.3.2 集合定义
    * @Param: []
    * @return: void
    * @Author: hfen
    * @Date: 2020/3/16 14:44
    */
    public static void myExportExcel2() {
        List<UserEntity> list = new ArrayList<>();
        int i =0;
        while (i<10) {
            UserEntity userEntity = new UserEntity();
            userEntity.setId(i+1);
            userEntity.setName("小寒" + i);
            userEntity.setAge(i);
            userEntity.setSex(1);
            userEntity.setEmail("asdfasdf");
            userEntity.setTime(new Date());
            list.add(userEntity);
            i++;
        }

        TeacherEntity teacherEntity = new TeacherEntity();
        teacherEntity.setId("1");
        teacherEntity.setName("张老师");

        List<CourseEntity> courseEntities = new ArrayList<>();
        for (int j = 0; j < 10; j++) {
            CourseEntity courseEntity = new CourseEntity();
            courseEntity.setId(String.valueOf(i+1));
            courseEntity.setName("数学");
            courseEntity.setMathTeacher(teacherEntity);
            courseEntity.setStudents(list);

            courseEntities.add(courseEntity);
        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("2412312", "测试", "测试"),
                CourseEntity.class, courseEntities);

        String path = "src/resources/excel/";
        String filename = "myCourse.xls";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
        }
        try (FileOutputStream fos = new FileOutputStream(path + filename)) {
            workbook.write(fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
    * @Description: 2.3.4 Excel导入介绍
    * @Author: hfen
    * @Date: 2020/3/16 14:46
    */
    public static void test2 () {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        long start = System.currentTimeMillis();
//        ExcelImportUtil.importExcel("src/resources/user.xls");

    }





    /**
    * @Description: 创建UserEntity list，导出Excel。导出单个sheet
    * @Param: []
    * @return: void
    * @Author: hfen
    * @Date: 2020/3/16 11:39
     * 参考 https://juejin.im/post/5d3eeb69e51d456206115981
    */
    public static void testExportExcel() throws IOException {
        List<UserEntity> list = new ArrayList<>();
        int i = 0;
        while (i<10) {
            UserEntity user = new UserEntity();
            user.setId(i + 1);
            user.setAge(i + 20);
            user.setEmail("82384@qq.com");
            user.setName("张三" + i);
            user.setSex(i%2==0?1:2);
            user.setTime(new Date());
            list.add(user);
            i++;
        }
        // 路径 采用项目路径 resources下； resources 在 main下
        EasyPoiUtils.exportExcel(UserEntity.class,list, "src/resources/excel/", "user.xls");
    }

    /**
    * @Description: 测试导出 多个sheet
    * @Param: []
    * @return: void
    * @Author: hfen
    * @Date: 2020/3/16 11:48
    */
    public static void testExportExcels() throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for(int n=1;n<4;n++){
            ExportParams exportParams = new ExportParams("用户信息"+n,"用户信息"+n);
            Object entity = UserEntity.class;
            List<UserEntity> data = new ArrayList<>();
            int i = 0;
            while (i < 10){
                UserEntity user = new UserEntity();
                user.setId(i*n+1);
                user.setAge(20+i);
                user.setEmail("abc@163.com");
                user.setName("张三"+i*n);
                user.setSex(i%2==0?1:2);
                user.setTime(new Date());
                data.add(user);
                i++;
            }
            // 构建map
            Map<String,Object> map = new HashMap<>();
            map.put("title",exportParams);
            map.put("entity",entity);
            map.put("data",data);
            list.add(map);
        }
        EasyPoiUtils.exportExcel(list,"src/resources/excel/","user1.xls");
    }

}
